<?php

namespace App\Http\Controllers\Resource;

use App\Remark;
use Illuminate\Http\Request;


use App\Http\Controllers\Controller;

//use Illuminate\Support\Facades\Request as RequestF;
use Illuminate\Support\Facades\Auth;

use App\Resource;
use App\Process;
use App\User;

use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\DB;

class ResourceController extends Controller
{
    /**
     *  能用的方法有 index create update destroy edit store
     */
    public function index(){

        $data = DB::table('resources')->get();

        $data = $this->resource_list($data);
//        dd($data);
        return view('resource.index')->withDatas($data);
    }

    /*
     * 重组资源列表项目,方便查看
     */
    public function resource_list($data){
        $process =  Process::lists('name','id')->toArray();
        $process[0] = '待操作';
        $user = User::lists('name','id')->toArray();
        $user[0] = '未定义';
        foreach($data as $k => $v){
            //赋值操作人
            $v->users_id = $user[$v->users_id];
            $v->yyzg_id = $user[$v->yyzg_id];
            $v->yyyg_id = $user[$v->yyyg_id];
            $v->mszg_id = $user[$v->mszg_id];
            $v->msyg_id = $user[$v->msyg_id];
            //赋值进程
            $v->process_id = $process[$v->process_id];
            $v->ms_process_id = $process[$v->ms_process_id];
            $v->yy_process_id = $process[$v->yy_process_id];
            $v->invite_status = $process[$v->invite_status];
            $v->reception_status = $process[$v->reception_status];
        }
        return $data;
    }

    //创建
    public function create(){
        $id = Auth::id();

        return view('resource.add')->withId($id);
    }

    //储存
    public function store(Request $request){

        $this->validate($request,[
            'phone' => 'bail|required|unique:resources',
//            'phone2' => 'different:phone',
            'name' => 'required|max:20',
//            'wechat' =>'sometimes|alpha_dash',
            'res_type_tid' => 'required',
            'res_resource_rid' => 'required',
        ]);

        $data = $_POST;
        $Resource = new Resource();
        $Resource->name = $data['name'];
        $Resource->phone = $data['phone'];
        $Resource->phone2 = $data['phone2'];
        $Resource->qq = $data['qq'];
        $Resource->wechat = $data['wechat'];
        $Resource->budget = $data['budget'];
        $Resource->users_id = $data['users_id'];
        $Resource->res_type_tid = $data['res_type_tid'];
        $Resource->res_resource_rid = $data['res_resource_rid'];

        $Resource->created = time();

        if($Resource->save()){
            return Redirect::to('/resource');
        }else{
            return Redirect::back()->withInput()->withErrors('添加失败!');
        }
    }

    //编辑
    public function edit($id){

        return view('resource.edit')->withData(Resource::find($id));
    }
    //更新
    public function update(Request $request,$id){
        $this->validate($request,[
            'phone' => 'bail|required|unique:resources,phone,'.$id.'|max:11',
            'name' => 'required|max:20',
        ]);

        $Resource = Resource::find($id);
        $data = $_POST;
        $Resource->name = $data['name'];
        $Resource->phone = $data['phone'];
        $Resource->phone2 = $data['phone2'];
        $Resource->qq = $data['qq'];
        $Resource->wechat = $data['wechat'];
        $Resource->budget = $data['budget'];
        $Resource->users_id = $data['users_id'];

        if($Resource->save()){
            return Redirect::to('/resource');
        }else{
            return Redirect::back()->withInput()->withErrors('修改失败!');
        }

    }
    //删除
    public function destroy($id){
        Resource::findOrFail($id)->delete();
        return redirect('/resource');
    }


    //邀约分配展示
    public function yy_assign($type){

        switch($type){
            case 'show':
                $data = DB::table('resources')->where('process_id',0)->get();       //获取未分配的资源
                $users = User::all();
                break;
            case 'done':
                $data = DB::table('resources')->where('process_id','>',0)->get();   //获取分配过后的资源
                $users = 0;
                break;
            case 'again':
                $data = DB::table('resources')->where('process_id',7)->get();       //获取进程为 7 的再约资源
                $users = User::all();
                break;
            case 'false':
                $data = DB::table('resources')->where('process_id',8)->get();       //获取进程为 8 的邀约失败资源
                $users = User::all();
                break;
            case 'outtime':
                $data = DB::table('resources')->where('process_id',3)->where('yyzg_time','<',strtotime('-1 week'))->get();       //获取分配成功但操作过期的资源
                $users = User::all();
                break;
            default:
                $data = DB::table('resources')->where('process_id',0)->get();       //默认获取未分配的资源
                $users = User::all();
                break;
        }


        $data = $this->resource_list($data);
//        dd($data);
        return view('resource.yy_assign')->withDatas($data)->withUsers($users)->withType('邀约资源分配')->withUrl('yy_assign_do')->withAction($type);
    }

    //主管确定分配
    public function yy_assign_do(Request $request){
        //增设一个判断,如果没选全就报错
        if(!isset($request->yyyg_id) || !isset($request->assign_id)){
            return Redirect::back()->withInput()->withErrors('请选择要分配的资源或者员工');
        }
        $fields = [                                                 //组合resource表中要更新的字段
            'yyyg_id'=>$request->yyyg_id,
            'process_id'=>3,
            'yyzg_id'=>Auth::id(),
            'yyzg_time'=>time(),
                    ];

        $resource = Resource::whereIn('id',$request->assign_id)->update($fields);   //更新资源表
        $data = [];
        foreach($request->assign_id as $id){                        //组合要添加到res_remarks表的数据 批量增加
            $data[] = [
                'created'=>time(),
                'des' => '邀约主管分配',
                'process_id' => 3,
                'users_id' =>Auth::id(),
                'resources_id' => $id,
            ];
        }
        $remarks = DB::table('res_remarks')->insert($data);
        if($remarks && $resource){
            return Redirect::back()->withInput()->withMessage('分配成功');
        }else{
            return Redirect::back()->withInput()->withErrors('分配失败');
        }
    }

    //电话邀约
    public function yy_invite($type){

        switch($type){
            case 'show':
                $data = DB::table('resources')->where('process_id',3)->where('yyzg_time','>=',strtotime('-1 week'))->where('yyyg_id',Auth::id())->get();
                break;
            case 'again':
                $data = DB::table('resources')->where('process_id',7)->where('yyyg_id',Auth::id())->get();  //获取再约的所有资源
                break;
            case 'againToday':
                $data = DB::table('resources')->where('process_id',7)->whereBetween('yy_time_again',[strtotime('today'),strtotime('tomorrow')])->where('yyyg_id',Auth::id())->get();  //获取再约的所有资源
                break;
            case 'againOuttime':
                $data = DB::table('resources')->where('process_id',7)->where('yyzg_time','<',strtotime('-1 week'))->where('yyyg_id',Auth::id())->get();  //获取再约的所有资源
                break;
            case 'done':
                $data = DB::table('resources')->where('invite_status','!=',0)->where('yyyg_id',Auth::id())->get();
                break;
            case 'nophone':
                $data = DB::table('resources')->where('process_id',3)->where('phone',0)->where('yyyg_id',Auth::id())->get();                        //无电话资源
                break;
            case 'outtime':
                $data = DB::table('resources')->where('process_id',3)->where('yyzg_time','<',strtotime('-1 week'))->where('yyyg_id',Auth::id())->get();     //过期资源
                break;
            default:
                $data = DB::table('resources')->where('process_id',3)->where('yyzg_time','>=',strtotime('-1 week'))->where('yyyg_id',Auth::id())->get();
                break;
        }


        $data = $this->resource_list($data);
        return view('resource.yy_invite')->withDatas($data)->withUrl('invite')->withType('电话邀约页')->withAction($type);
    }

    //门市分配展示
    public function ms_assign($type){
        echo $type;


        switch($type){
            case 'show'://待分配
                $data = DB::table('resources')->where('process_id',6)->whereBetween('schedule',[strtotime('-1 week'),strtotime('tomorrow')])->get();
                break;
            case 'done'://完成
                $data = DB::table('resources')->where('process_id','>',9)->get();
                break;
            case 'again'://再约       获取今天要再约的
                $data = DB::table('resources')->where('process_id',13)->whereBetween('ms_time_again',[strtotime('today'),strtotime('tomorrow')])->get();
                break;
            case 'false'://未成交
                $data = DB::table('resources')->where('process_id',12)->get();
                break;
            case 'outtime':
                $data = DB::table('resources')->where('process_id',6)->whereBetween('created',[strtotime('-1 week'),strtotime('tomorrow')])->get();
                break;
            case 'void':
                   //过期资源
                break;
            default:
                $data = DB::table('resources')->where('process_id',6)->get();
                break;
        }

        $users = User::all();
        $data = $this->resource_list($data);
        return view('resource.yy_assign')->withDatas($data)->withUsers($users)->withType('门市资源分配')->withUrl('ms_assign_do')->withAction($type);
    }

    //主管确定分配
    public function ms_assign_do(Request $request){
        //增设一个判断,如果没选全就报错
        if(!isset($request->yyyg_id) || !isset($request->assign_id)){
            return Redirect::back()->withInput()->withErrors('请选择要分配的资源或者员工');
        }
        $fields = [                                                 //组合resource表中要更新的字段
            'msyg_id'=>$request->yyyg_id,
            'process_id'=>9,
            'mszg_id'=>Auth::id(),
            'mszg_time'=>time(),
        ];

        $resource = Resource::whereIn('id',$request->assign_id)->update($fields);   //更新资源表
        $data = [];
        foreach($request->assign_id as $id){                        //组合要添加到res_remarks表的数据 批量增加
            $data[] = [
                'created'=>time(),
                'des' => '门市主管分配',
                'process_id' => 9,
                'users_id' =>Auth::id(),
                'resources_id' => $id,
            ];
        }
        $remarks = DB::table('res_remarks')->insert($data);
        if($remarks && $resource){
            return Redirect::back()->withInput()->withMessage('分配成功');
        }else{
            return Redirect::back()->withInput()->withErrors('分配失败');
        }
    }

    //门市接待
    public function ms_reception(){
        $data = DB::table('resources')->where('process_id',9)->where('msyg_id',Auth::id())->get();

        $users = User::all();
        $data = $this->resource_list($data);
        return view('resource.yy_invite')->withDatas($data)->withUsers($users)->withUrl('reception')->withType('进店接待页');
    }


}
